/**
 * Created by dungnt13 on 12/2/14.
 */
//var main_url = '/hocdans';
var main_url = '';
jQuery(document).ready(function () {

    // submit favourite action
    $('#favourite-this').on('click', function (e) {
        var elem = $(this);
        var id = elem.attr('data-id');
        var target = elem.attr('data-target');
        var options = {
            "id": id,
            "remove": 0,
            "target": target,
            "type": "favourite"
        };
        if (elem.hasClass('login'))
            showLoginForm();
        else {
            if (elem.hasClass('favourite_active'))
                options.remove = true;
            elem.activity(options);
        }
    });


    $('#thank-this').on('click', function (e) {
        var elem = $(this);
        var id = elem.attr('data-id');
        var target = elem.attr('data-target');
        var options = {
            "id": id,
            "remove": 0,
            "target": target,
            "type": "thank"
        };
        if (elem.hasClass('login'))
            showLoginForm();
        else {
            if (elem.hasClass('thank_active'))
                options.remove = true;
            elem.activity(options);
        }
    });

    $('.vote-this').on('click', function (e) {
        var elem = $(this);
        var id = elem.attr('data-id');
        var target = elem.attr('data-target');
        var options = {
            "id": id,
            "remove": 0,
            "target": target,
            "type": "vote"
        };
        if (elem.hasClass('login'))
            showLoginForm();
        else {
            if (elem.hasClass('vote_active'))
                options.remove = true;
            elem.activity(options);
        }
    });
    $('#join_course').on("click", function (e) {
        var elem = $(this);
        var id = elem.attr('data-id');
        var options = {
            "id": id
        };
        elem.joinCourse(options);
    });


    $('#a_btn_submit_search').on('click', function () {
        $('#form-search').submit();
    });

//    topQuestionAjax('');
    $('#top_qa_category a.cc').on('click', function () {
        var id = $(this).attr('data-value');
        topQuestionAjax(id);
    });

    $('#a_login_ajax').on('click', function () {
        $.ajax({
            url: main_url + '/signin-ajax',
            type: "POST",
            dataType: 'json',
            data: $('#stickyLoginForm').serialize(),
            success: function (data) {
                window.location.reload(true);
            }
        })
    });

});


function topQuestionAjax(id) {
    $.ajax({
        url: main_url + '/hoi-dap/top',
        data: {category: id},
        type: "POST",
        dataType: 'json',
        ifModified: false,
        success: function (data) {
            $('#top_question').html(data);
        }
    })
}

function listComments() {
    var id = $('#zone_comments').attr('data-id');
    var target = $('#zone_comments').attr('data-target');
    $.ajax({
        url: main_url + '/activities/list-comment',
        data: {target: target, id: id},
        type: "POST",
        dataType: 'json',
        ifModified: false,
        success: function (data) {
            $('#zone_comments').html(data);
        }
    })
}


function showLoginForm() {

}
